# Redis Cache 快速测试脚本 (PowerShell)
$BASE_URL = "http://localhost:3000/api/v1"

Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "    Redis Cache 功能测试" -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""

# 检查服务是否运行
Write-Host "1. 检查服务状态..." -ForegroundColor Yellow
try {
    $response = Invoke-WebRequest -Uri "$BASE_URL/" -Method GET -UseBasicParsing -TimeoutSec 2 -ErrorAction Stop
    Write-Host "✅ 应用运行正常" -ForegroundColor Green
} catch {
    Write-Host "❌ 错误: 应用未运行，请先启动应用 (pnpm start:dev)" -ForegroundColor Red
    exit 1
}
Write-Host ""

# 测试设置 token
Write-Host "2. 测试设置 token..." -ForegroundColor Yellow
try {
    $setResult = Invoke-RestMethod -Uri "$BASE_URL/redis/set" -Method POST
    if ($setResult -eq "OK") {
        Write-Host "✅ 设置成功: $setResult" -ForegroundColor Green
    } else {
        Write-Host "❌ 设置失败: $setResult" -ForegroundColor Red
    }
} catch {
    Write-Host "❌ 设置失败: $_" -ForegroundColor Red
}
Write-Host ""

Start-Sleep -Seconds 1

# 测试获取 token
Write-Host "3. 测试获取 token..." -ForegroundColor Yellow
try {
    $getResult = Invoke-RestMethod -Uri "$BASE_URL/redis/get" -Method GET
    Write-Host "📦 获取结果: $($getResult | ConvertTo-Json -Compress)" -ForegroundColor Cyan
    if ($getResult.token -or $getResult.expiringToken) {
        Write-Host "✅ 获取成功" -ForegroundColor Green
    } else {
        Write-Host "❌ 获取失败或数据为空" -ForegroundColor Red
    }
} catch {
    Write-Host "❌ 获取失败: $_" -ForegroundColor Red
}
Write-Host ""

# 测试设置带过期时间的 token
Write-Host "4. 测试设置带过期时间的 token..." -ForegroundColor Yellow
try {
    $setexResult = Invoke-RestMethod -Uri "$BASE_URL/redis/setex" -Method POST
    if ($setexResult -eq "OK") {
        Write-Host "✅ 设置成功: $setexResult" -ForegroundColor Green
    } else {
        Write-Host "❌ 设置失败: $setexResult" -ForegroundColor Red
    }
} catch {
    Write-Host "❌ 设置失败: $_" -ForegroundColor Red
}
Write-Host ""

# 测试获取 TTL
Write-Host "5. 测试获取 TTL..." -ForegroundColor Yellow
try {
    $ttlResult = Invoke-RestMethod -Uri "$BASE_URL/redis/ttl" -Method GET
    Write-Host "⏱️  TTL (毫秒): $ttlResult" -ForegroundColor Cyan
    if ($ttlResult -ne $null -and $ttlResult -ne -2 -and $ttlResult -ne -1) {
        Write-Host "✅ TTL 获取成功" -ForegroundColor Green
    } else {
        Write-Host "⚠️  TTL 可能无效或键不存在" -ForegroundColor Yellow
    }
} catch {
    Write-Host "❌ TTL 获取失败: $_" -ForegroundColor Red
}
Write-Host ""

# 最终验证
Write-Host "6. 最终验证 - 获取所有 token..." -ForegroundColor Yellow
try {
    $finalResult = Invoke-RestMethod -Uri "$BASE_URL/redis/get" -Method GET
    Write-Host "📦 最终结果: $($finalResult | ConvertTo-Json -Compress)" -ForegroundColor Cyan
} catch {
    Write-Host "❌ 获取失败: $_" -ForegroundColor Red
}
Write-Host ""

Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "    测试完成" -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan

